<%--
    Document   : index
    Author     : Thomas
--%>

<%@page import="WishlistPackage.WishListService"%>
<%@page import="WishlistPackage.WishList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="user" class="checkout.CheckoutInfo" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<jsp:useBean id="login" class="login.LoginInfo" scope="session"/>
<jsp:useBean id="member" class="login.userDBDriver" scope="session"/>

<%@ include file="header.jsp" %>

<%
    WishList wl = new WishListService().getWishListPort();

%>
<!-- Content -->

<div id="container">
	<div id="content" role="main">

                <div id="post-0" class="post error404 not-found">
			<h1 clasnes="entry-title">Wishlists</h1>
			<div class="entry-content">
                            <p>On this page you can allow other users to view books you have added to your Wishlist!
                            You can also view other users Wishlists.</p>
                            
                            <%
                                if(session.getAttribute("loggedin_username") != null){
                                    user.setAllNull();

                                    out.println("<h2>Your public Wishlist:</h2><p></p>");

                                    /*out.println("<table>");
                                    out.println("<tr><td><b>Name</b></td><td><b>Price (se Gr£)</b></td><td><b>Action</b></td></tr>");
                                    out.println("<tr><td colspan=3><font color='red'>Wishlist Service is currently down, please try again later!</font></td></tr>");
                                    out.println("</table>");
                                    */
                                    String clientBookList = "";
                                    clientBookList = new WishListService().getWishListPort().getList(session.getAttribute("loggedin_username").toString(), "9");
                                    //out.println(clientBookList);
                                    if (!clientBookList.contains("Invalid")) { //Absolute bodge because group 8 didnt bother error handling properly
                                        String listContentsString = clientBookList;
                                        String[] listContents = listContentsString.split(",");

                                        bookDatabase_pkg.bookDatabaseDriver driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        String categoryList1 = "action";
                                        int resultList1 = driverList1.databaseConnect(categoryList1);

                                        out.println("<table>");
                                        out.println("<tr><td><b>Name</b></td><td><b>Price (£)</b></td><td><b>Action</b></td></tr>");

                                        if(resultList1 == 0){
                                            for(int i=0; i<listContents.length; i++){
                                                String[] bookList1 = driverList1.getBook(listContents[i]);
                                                if(bookList1[0] != null){

                                                    out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                    out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                    out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                    out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                    out.println("</form>");
                                                }
                                            }
                                        }

                                        driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        categoryList1 = "drama";
                                        resultList1 = driverList1.databaseConnect(categoryList1);

                                        if(resultList1 == 0){
                                            for(int i=0; i<listContents.length; i++){
                                                String[] bookList1 = driverList1.getBook(listContents[i]);
                                                if(bookList1[0] != null){
                                                    out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                    out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                    out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                    out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                    out.println("</form>");
                                                }
                                            }
                                        }

                                        driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        categoryList1 = "comedy";
                                        resultList1 = driverList1.databaseConnect(categoryList1);

                                        if(resultList1 == 0){
                                            for(int i=0; i<listContents.length; i++){
                                                String[] bookList1 = driverList1.getBook(listContents[i]);
                                                if(bookList1[0] != null){
                                                    out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                    out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                    out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                    out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                    out.println("</form>");
                                                }
                                            }
                                        }

                                        out.println("</table>");
                                        }
                                    else
                                        {
                                            out.println("No books in your wishlist<p></p>");
                                        }
                                    out.println("<b>Books to add to your public Wishlist:</b><p></p>");


                                    if(session.getAttribute("list") != null){

                                        String listContentsString = (String) session.getAttribute("list");
                                        String[] listContents = listContentsString.split(",");

                                        bookDatabase_pkg.bookDatabaseDriver driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        String categoryList1 = "action";
                                        int resultList1 = driverList1.databaseConnect(categoryList1);

                                        out.println("<table>");
                                        out.println("<tr><td><b>Name</b></td><td><b>Price (£)</b></td><td><b>Action</b></td></tr>");

                                        if(resultList1 == 0){
                                            for(int i=0; i<listContents.length; i++){
                                                String[] bookList1 = driverList1.getBook(listContents[i]);
                                                if(bookList1[0] != null){

                                                    out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                    out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                    out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                    out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                    out.println("</form>");
                                                }
                                            }
                                        }

                                        driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        categoryList1 = "drama";
                                        resultList1 = driverList1.databaseConnect(categoryList1);

                                        if(resultList1 == 0){
                                            for(int i=0; i<listContents.length; i++){
                                                String[] bookList1 = driverList1.getBook(listContents[i]);
                                                if(bookList1[0] != null){
                                                    out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                    out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                    out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                    out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                    out.println("</form>");
                                                }
                                            }
                                        }

                                        driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                        categoryList1 = "comedy";
                                        resultList1 = driverList1.databaseConnect(categoryList1);

                                        if(resultList1 == 0){
                                            for(int i=0; i<listContents.length; i++){
                                                String[] bookList1 = driverList1.getBook(listContents[i]);
                                                if(bookList1[0] != null){
                                                    out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                    out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                    out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                    out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                    out.println("</form>");
                                                }
                                            }
                                        }

                                        out.println("</table>");
                                    }
                                    else{
                                        out.println("<table>");
                                        out.println("<tr><td><b>Name</b></td><td><b>Price (£)</b></td><td><b>Action</b></td></tr>");
                                        out.println("<tr><td colspan=3><font color='red'>There are no items on your Wishlist!</font></td></tr>");
                                        out.println("</table>");
                                    }
                                    
                                %>
                                <hr/>
                            </div><!-- .entry-content -->
                    </div><!-- #post-0 -->
                    <div id="post-0" class="post error404 not-found">
                            <h1 class="entry-title"></h1>
                            <div class="entry-content">
                                <h2>Other users Wishlists:</h2><p></p>
                                Select a user to view their Wishlist:

                                    <select onchange="showWishlist(this[selectedIndex].text)" name="userList">
                                    <option>Select User</option>
                                    <%
                                    int answercat = member.createCategoryDB("userDB");
                                    int answer1 = member.databaseConnect("userDB");

                                    String[] userArray = member.getAllUsers();
                                    if(!userArray[0].contains("File not read") && !userArray[0].contains("Client not connected") ){
                                        for(int i=0; i<userArray.length; i++){
                                            String[] userData = userArray[i].split("\t");
                                            String loggedInUser = (String) session.getAttribute("loggedin_username");
                                            if(!userData[0].contains("admin") && !userData[0].contains(loggedInUser)){
                                                out.println("<option value='" + userData[0] + "'>" + userData[0] + "</option>");
                                            }
                                        }
                                    }
                                    else{
                                        out.println("<option><font color='red'>Error connecting to User Database!</font></option>");
                                    }

                                    %></select><p></p><%

                                    userArray = member.getAllUsers();
                                    if(!userArray[0].contains("File not read") && !userArray[0].contains("Client not connected") ){
                                        out.println("<ul>");
                                        for(int i=0; i<userArray.length; i++){
                                            String[] userData = userArray[i].split("\t");
                                            if(!userData[0].contains("admin")){
                                                out.println("<li id='" + userData[0] + "'>" + userData[0] + "'s Wishlist:");
                                                 String clientBookList2 = "";
                                                                                                 clientBookList2 = new WishListService().getWishListPort().getList(userData[0], "9");
                                                                                                 //out.println(clientBookList);
                                                                                                 if (!clientBookList.contains("Invalid")) { //Absolute bodge because group 8 didnt bother error handling properly
                                                                                                     String listContentsString = clientBookList;
                                                                                                     String[] listContents = listContentsString.split(",");

                                                                                                     bookDatabase_pkg.bookDatabaseDriver driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                                                                                     String categoryList1 = "action";
                                                                                                     int resultList1 = driverList1.databaseConnect(categoryList1);

                                                                                                     out.println("<table>");
                                                                                                     out.println("<tr><td><b>Name</b></td><td><b>Price (£)</b></td><td><b>Action</b></td></tr>");

                                                                                                     if (resultList1 == 0) {
                                                                                                         for (int j = 0; j < listContents.length; j++) {
                                                                                                             String[] bookList1 = driverList1.getBook(listContents[j]);
                                                                                                             if (bookList1[0] != null) {

                                                                                                                 out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                                                                                 out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                                                                                 out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                                                                                 out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                                                                                 out.println("</form>");
                                                                                                             }
                                                                                                         }
                                                                                                     }

                                                                                                     driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                                                                                     categoryList1 = "drama";
                                                                                                     resultList1 = driverList1.databaseConnect(categoryList1);

                                                                                                     if (resultList1 == 0) {
                                                                                                         for (int j = 0; j < listContents.length; j++) {
                                                                                                             String[] bookList1 = driverList1.getBook(listContents[j]);
                                                                                                             if (bookList1[0] != null) {
                                                                                                                 out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                                                                                 out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                                                                                 out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                                                                                 out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                                                                                 out.println("</form>");
                                                                                                             }
                                                                                                         }
                                                                                                     }

                                                                                                     driverList1 = new bookDatabase_pkg.bookDatabaseDriver();
                                                                                                     categoryList1 = "comedy";
                                                                                                     resultList1 = driverList1.databaseConnect(categoryList1);

                                                                                                     if (resultList1 == 0) {
                                                                                                         for (int j = 0; j < listContents.length; j++) {
                                                                                                             String[] bookList1 = driverList1.getBook(listContents[i]);
                                                                                                             if (bookList1[0] != null) {
                                                                                                                 out.println("<tr><td>" + bookList1[1] + "</td><td>" + bookList1[3] + "</td>");

                                                                                                                 out.println("<form name='addBk" + bookList1[0] + "' role='removeFromCart' method='get' id='searchform' action='removeFromList.jsp'>");
                                                                                                                 out.println("<input type='hidden' value=" + bookList1[0] + " name='ISBN' id='ISBN' />");
                                                                                                                 out.println("<td><a href='' onclick='document.addBk" + bookList1[0] + ".submit(); return false;'>(add)</a></td>");
                                                                                                                 out.println("</form>");
                                                                                                             }
                                                                                                         }
                                                                                                     }

                                                                                                     out.println("</table>");
                                                                                                 } else {
                                                                                                     out.println("No books in wishlist<p></p>");
                                                                                                 }
                                                // Script to hide tables until selected
                                                out.println("<script type='text/javascript'>document.getElementById('" + userData[0] + "').style.display='none';</script>");
                                            }
                                        }
                                        out.println("<ul>");
                                    }
                                    else{
                                        out.println("<option><font color='red'>Error connecting to User Database!</font></option>");
                                    }
                                }
                                //If user is not logged in asks to log in before proceeding
                                else{
                                    session.setAttribute("currentPage", request.getRequestURL()+"?wishlist");
                                    out.println("Please sign in to proceed:"); %>
                                    <p></p>
                                    <form action="dologin.jsp" method=post>
                                    <table>
                                        <tr><td>Username: </td><td colspan=2><input type="text" name="username" value="" size=20  maxlength=50></td></tr>
                                        <tr><td>Password: </td><td colspan=2><input type="password" name="password" value="" size=20  maxlength=20></td></tr>
                                        <tr><td colspan=2></td><td><a HREF="checkout.jsp"><input type="submit" value="Submit"></a> <input type="reset"  value="Reset"></td></tr>
                                    </table>
                                    </form>
                                <% } %>
                        </div><!-- .entry-content -->
		</div><!-- #post-0 -->
                </div><!-- #content -->
</div><!-- #container -->

<%@ include file="sidebar.jsp" %>

<!-- /Content -->

<%@ include file="footer.jsp" %>
